import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/layout/index.vue";
import Admin from "@/views/admin/index.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/register",
    name: "register",
    component: () => import("@/views/register/index.vue"),
  },
  {
    path: "/articleDetail",
    name: "articleDetail",
    component: () => import("@/views/public/articleDetail.vue"),
  },
  {
    path: "/user/postArticle",
    name: "postArticle",
    component: () => import("@/views/user/articleManage/postArticle.vue"),
  },
  {
    path: "/",
    name: "home",
    component: Layout,
    children: [
      {
        path: "/",
        name: "index",
        component: () => import("@/views/index.vue"),
      },
      {
        path: "/user/inf",
        name: "inf",
        component: () => import("@/views/user/infManage/infManage.vue"),
      },
      {
        path: "/user/updatepwd",
        name: "updatepwd",
        component: () => import("@/views/user/infManage/updatePwd.vue"),
      },
      {
        path: "/user/myFavorite",
        name: "myFavorite",
        component: () => import("@/views/user/infManage/myFavorite.vue"),
      },
      {
        path: "/user/myFollow",
        name: "myFollow",
        component: () => import("@/views/user/infManage/myFollow.vue"),
      },
      {
        path: "/user/myLike",
        name: "myLike",
        component: () => import("@/views/user/infManage/myLike.vue"),
      },
      {
        path: "/user/myArticle",
        name: "articleManage",
        component: () => import("@/views/user/articleManage/myArticle.vue"),
      },
    ],
  },
  {
    path: "/admin",
    name: "admin",
    component: Admin,
    children: [
      {
        path: "/",
        name: "screen",
        component: () => import("@/views/admin/articleManage/articleOverview"),
      },
      {
        path: "/admin/userManage",
        name: "user",
        component: () => import("@/views/admin/userManage/index.vue"),
      },
      {
        path: "/admin/articleManage",
        name: "article",
        component: () => import("@/views/admin/articleManage/index.vue"),
      },
      {
        path: "/admin/category",
        name: "category",
        component: () => import("@/views/admin/articleManage/Category.vue"),
      },
      {
        path: "/admin/inf",
        name: "inf",
        component: () => import("@/views/admin/infManage/index.vue"),
      },
      {
        path: "/admin/updatepwd",
        name: "updatepwd",
        component: () => import("@/views/admin/infManage/updatepwd.vue"),
      },
    ],
  },
  {
    path: "/403",
    name: "403",
    component: () => import("@/views/public/403"),
  },
  {
    path: "/NotLogin",
    name: "NotLogin",
    component: () => import("@/views/public/NotLogin"),
  },
  {
    path: "*",
    name: "404",
    component: () => import("@/views/public/404"),
  },
];

const router = new VueRouter({
  mode: "history", // 启用历史模式
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  // console.log(to);
  // console.log(from);
  // to 是到达的路游信息
  // from 是开始的路游信息
  // next 是帮助我们跳转路游的函数
  const adminPaths = ["/admin", "/admin/*"];
  const user = JSON.parse(localStorage.getItem("user") || "{}");
  if (user.role !== "admin" && adminPaths.includes(to.path)) {
    //如果当前用户访问页面的权限不够(非管理员),则跳转到"权限不足"页面
    next("/403");
  } else {
    next();
  }
  next();
});

export default router;
